<?php
$isReloadable = false;
require_once("../../pers/inc.config.php");
require_once(OVF_FS_WEB_ROOT.'/app/_inc/inc.session.php');
require_once(OVF_FS_WEB_ROOT.'/app/_inc/funct.html.php');
$objSess->setReqSV('agenda', 'dataAge', date('d/m/Y'));
$objSess->setReqSV('agenda', 'agende_txt', $objSess->nome);
reqDef('data_memo_fine','31/12/2030');
reqDef('pub_occ_pri','pub');

reqDef('listIdAge',$objSess->getSV('agenda','listAge'));
$sSql = "SELECT u.nome, u.cognome FROM ovf_utenti u "
	." WHERE u.id_utente IN (".((reqVar('listIdAge'))?reqVar('listIdAge'):'0').") ";
//dbg_($sSql);
$objDB->query($sSql,'selUte');	
while($acX = $objDB->fetch('selUte')) {
	$aNomiAge[] = $acX['nome']." ".$acX['cognome'];
}
$sListNomiAge = implode(", ",$aNomiAge);

if (reqVar('azione') == 'salva') {
	$msgPag = confIns();
} 
$aOre = array('08'=>'08','09'=>'09','10'=>'10','11'=>'11','12'=>'12','13'=>'13','14'=>'14','15'=>'15'
	,'16'=>'16','17'=>'17','18'=>'18','19'=>'19','20'=>'20','21'=>'21','22'=>'22','23'=>'23'
	,'00'=>'00','01'=>'01','02'=>'02','03'=>'03','04'=>'04','05'=>'05','06'=>'06','07'=>'07');
$aMin = array('00'=>'00','15'=>'15','45'=>'45');
$aRipeti = array(''=>'non ripetere','a'=>'ogni anno','m'=>'ogni mese','g'=>'ogni giorno');
$aPOP = array('pub'=>'pubblico','occ'=>'occupato','pri'=>'privato');

$agendeTxt = $objSess->nome;
$sCmdR = '';
$sCmdR .= "&nbsp;&nbsp;&nbsp;";
$sCmdR .= "<a class=\"cmd1\" href=\"javascript:document.frm1.submit();\" >salva</a>\n";
$sCmdR .= cmdTorna('torna'); 

testaHtml('Agenda','Nuovo memo',$msgPag,'agen');
?>
<script type="text/javascript">
<!--
function apriAgenModSelez() {
   window.open('agenModSelez.php?listIdAge='+document.frm1.listIdAge.value
      +'&xsn=<?=XSN?>','agenSelez'
      ,'top=200,left=200,height=600,width=700,scrollbars=yes,resizable=yes');
}
//-->
</script>
<table border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
   <tr>
      <td class="cmd1" align="right"><?php echo $sCmdR;?></td>
   </tr>
</table>
<?=getHtmlForm('frm1','',array('enctype'=>true));?>
<?=getHtmlHidden('azione','salva');?>
<?=getHtmlHidden('xsn',XSN);?>
<table class="tab1" border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
	<?=getHtmlRigaRif();?>
   <tr>
      <td class="tit1" colspan='20' align='center'>Memo</td>
   </tr>
   <tr>
      <td class="lab1" colspan='3' align='left'>
      	<a class='lab1' href='javascript:apriAgenModSelez()'><?=_t("In agenda")?></a>
      </td>
      <td class="txt1" colspan='15' align='left'>
      	<?php echo getHtmlHidden('listIdAge');?>
      	<div id='listNomiAge'><?=$sListNomiAge?></div>
		</td>
      <td class="txt1" colspan='2' align='left'>
      	<?php echo getHtmlSelect('pub_occ_pri',$aPOP);?>
		</td>
   </tr>
   <tr>
      <td class="lab1" colspan='3' align='left'>Oggetto</td>
      <td class="txt1" colspan='17' align='left'>
      	<?php echo getHtmlText('oggetto',100)?>
		</td>
   </tr>
   <tr>
      <td class="lab1" colspan='3' align='left'>Data</td>
      <td class="txt1" colspan='3' align='left'>
      	<?php echo getHtmlDate('dataAge')?>
		</td>
      <td class="lab1" colspan='1' align='left'>Ora</td>
      <td class="txt1" colspan='1' align='left'>
      <?php echo getHtmlSelect('ora',$aOre)?>
		</td>
      <td class="lab1" colspan='1' align='left'>Minuti</td>
      <td class="txt1" colspan='1' align='left'>
      <?php echo getHtmlSelect('minuti',$aMin)?>
		</td>
      <td class="lab1" colspan='1' align='left'>Ripeti</td>
      <td class="txt1" colspan='4' align='left'>
      <?php echo getHtmlSelect('ripeti',$aRipeti)?>
		</td>
      <td class="lab1" colspan='1' align='left'>Fino a</td>
      <td class="txt1" colspan='4' align='left'>
      <?php echo getHtmlDate('data_memo_fine')?>
		</td>
	</tr>
   <tr>
      <td class="lab1" colspan='3' align='left'>Nota</td>
      <td class="txt1" colspan='17' align='left'>
      <?php echo getHtmlTextArea('nota',100,10)?>
		</td>
   </tr>
</table>
<table border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
   <tr>
      <td class="cmd1" align="right"><?php echo $sCmdR;?></td>
   </tr>
</table>
<?
echo "</form>\n";
codaHtml();
exit();
function confIns() {
	global $objDB, $objSess;
	$msg = '';
	if (trim(reqVar('listIdAge'))=='') {
		$msg .= _t("selezionare almeno un'agenda")."<br>";
	}
	if (trim(reqVar('oggetto'))=='') {
		$msg .= _t("oggetto obbligatorio<br>")."<br>";
	}
	if (reqVarStr2date('dataAge')===false) {
		$msg .= _t("data errata")."<br>";
	}
	if (reqVarStr2date('data_memo_fine')===false) {
		$msg .= _t("data fine errata")."<br>";
	}
	if (reqVarStr2date('data_memo_fine') < reqVarStr2date('data_memo_iniz')) {
		$msg .= _t("data memo successiva a data fine")."<br>";
	}
	if ($msg != '') {
		return $msg;
	}
	$anno = date('Y',reqVarStr2date('dataAge'));
	$mese = date('m',reqVarStr2date('dataAge'));
	$giorno = date('d',reqVarStr2date('dataAge'));
	if (reqVar('ripeti')=='a') {
		$anno = 0;
		$mese = date('m',reqVarStr2date('dataAge'));
		$giorno = date('d',reqVarStr2date('dataAge'));
		$dataMemoIniz = reqVarStr2YMD('dataAge');
		$dataAgenda = reqVarStr2YMD('data_memo_fine');
	} elseif (reqVar('ripeti')=='m') {
		$anno = 0;
		$mese=0;
		$giorno = date('d',reqVarStr2date('dataAge'));
		$dataMemoIniz = reqVarStr2YMD('dataAge');
		$dataAgenda = reqVarStr2YMD('data_memo_fine');
	}elseif (reqVar('ripeti')=='g') {
		$anno = 0;
		$mese=0;
		$giorno= 0;
		$dataMemoIniz = reqVarStr2YMD('dataAge');
		$dataAgenda = reqVarStr2YMD('data_memo_fine');
	} else {
		$anno = date('Y',reqVarStr2date('dataAge'));
		$mese = date('m',reqVarStr2date('dataAge'));
		$giorno = date('d',reqVarStr2date('dataAge'));
		$dataMemoIniz = $dataAgenda = reqVarStr2YMD('dataAge');
	}

	$sSqlIns = "INSERT INTO ovf_agenda SET tipo_agenda = 'M' "
		.", anno = $anno, mese = $mese, giorno = $giorno"
		.", ora = ".reqVarAS('ora').", minuti = ".reqVarAS('minuti')
		.", data_agenda = '$dataAgenda'"
		.", data_memo_iniz = '$dataMemoIniz'"
		.", oggetto = '".reqVarAS('oggetto')."'"
		.", pub_occ_pri = '".reqVarAS('pub_occ_pri')."'"
		.", nota = '".reqVarAS('nota')."'"
		.", id_utente_ins = ".$objSess->idUtente;
	$objDB->exec($sSqlIns,'insRec');
	$newId = $objDB->lastInsertId('insRec');
	
	$aX = explode(',',reqVar('listIdAge'));
	foreach($aX as $idX) {
		$sSql = "INSERT INTO ovf_agenda_utenti "
		." SET id_agenda = $newId, id_utente = $idX";
		$objDB->exec($sSql);
	}
	
	header("Location: ".urlTorna(_t("memo inserito")));
	exit();
}
